package com.niit.gams.controller;

import com.github.pagehelper.PageInfo;
import com.niit.gams.pojo.SysResource;
import com.niit.gams.pojo.SysUser;
import com.niit.gams.pojo.TreeNode;
import com.niit.gams.service.SysResourceService;
import com.niit.gams.service.SysUserService;
import com.niit.gams.utils.TreeUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/users")
public class SysUserController {
  private SysUserService sysUserService;
  private SysResourceService sysResourceService;

  public SysUserController(SysUserService sysUserService, SysResourceService sysResourceService) {
    this.sysUserService = sysUserService;
    this.sysResourceService = sysResourceService;
  }

  @GetMapping("/login")
  public ModelAndView login(SysUser user) {
    ModelAndView mav = new ModelAndView();
    //获取当前用户
    Subject subject = SecurityUtils.getSubject();
    //封装用户信息生成token令牌
    UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
    //执行登录操作,可以自定义捕获异常
    try {
      //执行UsrRealm类的doGetAuthenticationInfo()
      subject.login(token);
      subject.getSession().setAttribute("user",user);
      mav.setViewName("index");
      List<SysResource> resources = sysResourceService.findAll();
      List<TreeNode> nodeList = new ArrayList<>();
      for (SysResource resource:resources) {
        if(resource.getType().equals("button")){
          continue;
        }
        TreeNode node = new TreeNode();
        node.setId(resource.getId());
        node.setName(resource.getName());
        node.setPId(resource.getParentId());
        node.setPermission(resource.getPermission());
        node.setUrl(resource.getUrl());
        node.setIcon(resource.getIcon());
        nodeList.add(node);
      }
      TreeUtil util = new TreeUtil(nodeList);
      nodeList = util.buildTree();
      mav.addObject("menuList",nodeList);
      System.out.println(nodeList);
    } catch (UnknownAccountException e) {
      mav.addObject("msg", "用户名不存在");
      mav.setViewName("login");
    } catch (IncorrectCredentialsException e) {
      mav.addObject("msg", "密码有误");
      mav.setViewName("login");
    }
    return mav;
  }


  @GetMapping
  public ResponseEntity<PageInfo<SysUser>> findUsersByPage(@RequestParam(defaultValue = "1") Integer curPage, @RequestParam(defaultValue = "2") Integer pageSize) {
    PageInfo<SysUser> pageInfo = sysUserService.findUsersByPage(curPage, pageSize);
    return ResponseEntity.ok(pageInfo);
  }

  @PostMapping
  public ResponseEntity<Boolean> addUser(SysUser user){
    boolean flag = sysUserService.addUser(user);
    return ResponseEntity.ok(flag);
  }

  @DeleteMapping("/{id}")
  public ResponseEntity<Boolean> delUser(@PathVariable("id") Integer id){
    boolean flag = sysUserService.delUser(id);
    return ResponseEntity.ok(flag);
  }

  @PutMapping
  public ResponseEntity<Boolean> updateUser(SysUser user){
    boolean flag = sysUserService.updateUser(user);
    return ResponseEntity.ok(flag);
  }
}